#include "Sys_local.h"
#include "../framework/CompileConfig.h"
#include <sys/stat.h>

infSys_Local sysLocal;
infSys* sys = &sysLocal;

infSys_Local::infSys_Local()
{

}

infSys_Local::~infSys_Local()
{

}

void infSys_Local::DebugPrintf( const char *fmt, ... )
{

}

void infSys_Local::DebugVPrintf( const char *fmt, va_list arg )
{

}

float infSys_Local::GetClockTicks( void )
{
    LARGE_INTEGER li;

    QueryPerformanceCounter( &li );
    return static_cast<float>( (double ) li.LowPart + (double) 0xFFFFFFFF * li.HighPart );
}

float infSys_Local::ClockTicksPerSecond( void )
{
    static double ticks = 0;

    if ( !ticks ) {
        LARGE_INTEGER li;
        QueryPerformanceFrequency( &li );
        ticks = static_cast<double>(li.QuadPart);
    }

    return static_cast<float>(ticks);
}

union Time_t {
    long long tLong;
    time_t tTime;
};

long long infSys_Local::GetTimeStamp( void* o ) {
    Time_t tt;
    FILETIME createTime, lastATime, lastWTime;
    HANDLE hFile = (HANDLE) _get_osfhandle(_fileno((FILE*)o));;
    ::GetFileTime(hFile, &createTime, &lastATime, &lastWTime);
    memcpy(&tt.tTime, &lastWTime, sizeof(lastWTime));
    return tt.tLong;
}

const char* infSys_Local::GetExePath() const
{	
    static char pStrDir[512]={0};
    int num = ::GetCurrentDirectoryA(512, pStrDir);
    if(num==0)
        return 0;
    return pStrDir;
}

static const UINT MB_TYPES[3] = 
{
    MB_ICONINFORMATION,
    MB_ICONWARNING,
    MB_ICONERROR
};



static const HWND DEFAULT_HWND=0;

int infSys_Local::MessageBoxChar( const char* title, const char* text, messageBoxType_t type )
{
    return ::MessageBoxA(DEFAULT_HWND, text, title, MB_TYPES[type] | MB_OK);
}

int infSys_Local::MessageBoxWChar( const wchar_t* title, const wchar_t* text, messageBoxType_t type )
{
    return ::MessageBoxW(DEFAULT_HWND, text, title, MB_TYPES[type] | MB_OK);
}



const char* Sys_GetAppName() {
    static char strText[INF_MAX_STRING_CHARS]={0};

    if( strText[0] == '\0' )
        GetModuleFileName( NULL, strText, INF_MAX_STRING_CHARS );

    return strText;
}
